
from time import sleep
from pyx_package.script import BaseBehavior
from pyx_package.ps_paramete import PsParameter
PYX_MODULE_NAME = "list-extract"

class ScriptBehavior(BaseBehavior):

    def __init__(self) -> None:
        super().__init__()
        
        self.SetExeMode("function")
        
        self.InitInputParam()
        
        self.InitOutputParam()

        #默认展示一个元素
        self.SetShowEleCount(1)
        
    def InitInputParam(self):
    
        self.ip_list = self.CreateParam(
            "list", "提取列表", PsParameter.Input, list, [])
            
        #列表元素数量
        self.ip_ele_count = self.CreateParam(
            "ele_count", "元素数量", PsParameter.Input, int, 1)
        self.ip_ele_count.SetValueChangedFunc("EleChagned")

    def InitOutputParam(self):

        self._ele_list = []

        #下发元素参数
        self.ele_0 = self.CreateParam("ele_0", "元素0", PsParameter.Output, any)
        self.ele_1 = self.CreateParam("ele_1", "元素1", PsParameter.Output, any)
        self.ele_2 = self.CreateParam("ele_2", "元素2", PsParameter.Output, any)
        self.ele_3 = self.CreateParam("ele_3", "元素3", PsParameter.Output, any)
        self.ele_4 = self.CreateParam("ele_4", "元素4", PsParameter.Output, any)
        self.ele_5 = self.CreateParam("ele_5", "元素5", PsParameter.Output, any)
        self.ele_6 = self.CreateParam("ele_6", "元素6", PsParameter.Output, any)
        self.ele_7 = self.CreateParam("ele_7", "元素7", PsParameter.Output, any)
        self.ele_8 = self.CreateParam("ele_8", "元素8", PsParameter.Output, any)
        self.ele_9 = self.CreateParam("ele_9", "元素9", PsParameter.Output, any)

        #将所有端口统一管理，方便隐藏
        self._ele_list.append(self.ele_0)
        self._ele_list.append(self.ele_1)
        self._ele_list.append(self.ele_2)
        self._ele_list.append(self.ele_3)
        self._ele_list.append(self.ele_4)
        self._ele_list.append(self.ele_5)
        self._ele_list.append(self.ele_6)
        self._ele_list.append(self.ele_7)
        self._ele_list.append(self.ele_8)
        self._ele_list.append(self.ele_9)

    def SetShowEleCount(self, count):
        '''
            设置输出的元素数量
        '''
        for index in range(len(self._ele_list)):

            if index < count:
                self._ele_list[index].hide = False
            
            else:
                self._ele_list[index].hide = True

    def EleChagned(self):
        '''
            当元素数量参数改变时回调
        '''
        #获取新的数量，设置进来
        self.SetShowEleCount(self.ip_ele_count.value)

    def FirstStep(self):
        
        #判断传进来的列表数量是否与设置的一致
        if len(self.ip_list.value) != self.ip_ele_count.value:
            self.Error("输出列表数量与设置的输出数量不一致！")

        
    def DoStep(self):

        cur_index = 0
        
        #取出输入列表元素，设置给输出元素
        for ele in self.ip_list.value:
            self._ele_list[cur_index].value = ele
            cur_index += 1
